package com.pandora.credit.mongo.entity;

import com.pandora.common.util.VerifyUtil;
import com.pandora.credit.exception.InvalidDataException;

import java.util.ArrayList;
import java.util.List;

/**
 * @author developer
 */
public class OpConvertReportFormat extends AbstractReportAfterFormat {
    private static final int FEE_SUMMARY_LIMIT_LENGTH = 6;
    private static final int CONTACT_REGION_SUMMARY_LIMIT_LENGTH = 5;
    private static final int CALL_DATA_SUMMARY_LIMIT_LENGTH = 5;


    @Override
    protected ReportMetaData getMetaDataByReport(AbstractReport report) {
        OpReport opReport = (OpReport)report;
        ReportMetaData metaData = new ReportMetaData();
        OpDataSource ds = opReport.getDataSource();
        String mobile;
        long reportId;
        if (ds != null) {
            reportId = opReport.getReportId();
            if (reportId <= 0) {
                logger.info("no report id in the report");
                throw new InvalidDataException("报告中未找到用报告id!");
            }
            metaData.setReportId(reportId);

            mobile = ds.getMobile();
            if (VerifyUtil.isEmpty(mobile)) {
                logger.info("no mobile in the report");
                throw new InvalidDataException("报告中未找到用户手机信息!");
            }
            metaData.setMobile(mobile);
        } else {
            logger.info("no data source in the report");
            throw new InvalidDataException("报告中未找到DataSource!");
        }
        return metaData;
    }

    @Override
    protected AbstractReport dealReportData(AbstractReport report) {
        OpReport opReport = (OpReport)report;
        if (opReport.getContactAndAddressList() != null) {
            opReport.setContactAndAddressList(null);
        }

        if (opReport.getCallDataSummaryList() != null) {
            List<CallDataSummary> callDataSummaries = new ArrayList<>();
            for (CallDataSummary summary : opReport.getCallDataSummaryList()) {
                String title = summary.getTitle();
                if ("常用联系人".equals(title)) {
                    List<CallDataSummaryDetail> callDataSummaryDetails =  summary.getCallDataSummaryDetailList();
                    if (callDataSummaryDetails != null && callDataSummaryDetails.size() > CALL_DATA_SUMMARY_LIMIT_LENGTH) {
                        summary.setCallDataSummaryDetailList(callDataSummaryDetails.subList(0,CALL_DATA_SUMMARY_LIMIT_LENGTH));
                    }
                    callDataSummaries.add(summary);
                }
            }
            opReport.setCallDataSummaryList(callDataSummaries);
        }

        if(opReport.getFeeSummaryList() != null) {
            if(opReport.getFeeSummaryList().size() > FEE_SUMMARY_LIMIT_LENGTH) {
                opReport.setFeeSummaryList(opReport.getFeeSummaryList().subList(0,FEE_SUMMARY_LIMIT_LENGTH));
            }
        }

        if(opReport.getContactRegionSummaryList() != null) {
            if(opReport.getContactRegionSummaryList().size() > CONTACT_REGION_SUMMARY_LIMIT_LENGTH) {
                opReport.setContactRegionSummaryList(opReport.getContactRegionSummaryList().subList(0,CONTACT_REGION_SUMMARY_LIMIT_LENGTH));
            }
        }
        return opReport;
    }
}
